<?php
/* 
 * ********* CVS HEADER ********* 
 * 
 * $RCSfile: admseqsauv.php,v $ 
 * $Date: 2005/10/21 12:58:07 $ 
 * $Revision: 1.3 $ 
 * 
 * ********* CVS HEADER ********* 
 */
require_once('module/TPLN/TPLN.php');
$TPLN = new TPLN();
if($action=="process"){
	$message="";
	$TPLN->Open($op_basepath."tpl/admseqsauv.tpl");
	
	$seq1_query = "";
	$seq1 = array();
	if(is_array($sequences)){
		$i=0;
		while($sequences[$i]){
			$seq1[$i] = "id='".$sequences[$i]."'";
			$i++;	
		}
		$seq1_query = implode(" OR ",$seq1);
	}
	if($seq1_query != ''){
		$query = "SELECT * FROM sequence WHERE ".$seq1_query;
		$result=rportalrequete($query) or rportalerreur($query,"?op=");
		if(rportalnbresult($result)==0){
		    $TPLN->EraseBloc("sequence");
		}else{
		    while($seq=rportalligne($result)){
		    	$message.="Sequence ".$seq['nom']." ajoutee.\n";
		    	$query="SELECT id_etape,nom FROM sequence_ordre,sequence_etape WHERE id_sequence='".$seq['id']."' AND sequence_etape.id=id_etape";
		    	$result2=rportalrequete($query) or rportalerreur($query,"?op=");
		    	if(rportalnbresult($result2)==0){
				    $TPLN->EraseBloc("sequence.ordre");
				}else{
				    while($script=rportalligne($result2)){
				    	$message.="script ".$script['nom']." ajoute dans la sequence ".$seq['nom'].".\n";
				    	$TPLN->Parse("sequence.ordre.nom_script",$script['nom']);
				    	$TPLN->Loop("sequence.ordre");
				    }
		    	}
				$TPLN->Parse("sequence.nom",$seq['nom']);
				$TPLN->Parse("sequence.description",$seq['description']);
				$TPLN->Loop("sequence");
		    }
		}
	}else{
		$TPLN->EraseBloc("sequence");	
	}
	$s1 = array();
	$script_query='';
	if(is_array($scripts)){
		$i=0;
		while($scripts[$i]){
			$s1[$i] = "id='".$scripts[$i]."'";
			$i++;	
		}
		$script_query = implode(" OR ",$s1);
	}
	if($script_query!=''){
		$query="SELECT * FROM sequence_etape WHERE ".$script_query;
		$result=rportalrequete($query) or rportalerreur($query,"?op=");
		if(rportalnbresult($result)==0){
		    $TPLN->EraseBloc("script");
		}else{
		    while($script=rportalligne($result)){
		    	$message.="le script ".$script['nom']." a ete ajoute.\n";
		    	$TPLN->Parse("script.nom",$script['nom']);
		    	$TPLN->Parse("script.description",$script['description']);
		    	$TPLN->Parse("script.fichier",$script['fichier']);
		    	$TPLN->Loop("script");
		    }
		}
	}else{
		$TPLN->EraseBloc("script");	
	}
	$nom=time();
	$fichier=fopen("install/sequence$nom.php","w");
	fputs($fichier,$TPLN->Output());
	fclose($fichier);
	$TPLN->Open($op_basepath."tpl/admseqsauv.html");
	$TPLN->IncludeFile("header","environnement/".$session['environnement']."/header.php"); 
	$TPLN->IncludeFile("footer","environnement/".$session['environnement']."/footer.php");
	
	require_once(get_path_module("admin"));
	$pc = new panneau_controle();
	$pc->calculRubriqueParFonction($op);
	$TPLN->Parse("panneau_controle",$pc->retourneEntete());
	
	$TPLN->Parse("fichier","install/sequence$nom.php");
	$TPLN->Parse("message",nl2br($message));
	$TPLN->Write();
	exit;
}
$TPLN->Open($op_basepath."tpl/admseqsauvform.html");
$query="SELECT * FROM sequence_etape ORDER BY fichier";
$result=rportalrequete($query) or rportalerreur($query,"?op=");
if(rportalnbresult($result)==0){
    $TPLN->EraseBloc("script");
}else{
    while($script=rportalligne($result)){
		$TPLN->Parse("script.scriptid",$script['id']);
		$TPLN->Parse("script.nom",$script['nom']);
		$TPLN->Parse("script.description",$script['description']);
		$TPLN->Parse("script.fichier",$script['fichier']);
		$TPLN->Loop("script");
    }
}
$query="SELECT * FROM sequence ORDER BY nom";
$result=rportalrequete($query) or rportalerreur($query,"?op=");
if(rportalnbresult($result)==0){
    $TPLN->EraseBloc("liste");
}else{
    while($seq=rportalligne($result)){
		$TPLN->Parse("liste.sequenceid",$seq['id']);
		$TPLN->Parse("liste.description",$seq['description']);
		$TPLN->Parse("liste.nom",$seq['nom']);
		$TPLN->Loop("liste");
    }
}
$TPLN->IncludeFile("header","environnement/".$session['environnement']."/header.php"); 
$TPLN->IncludeFile("footer","environnement/".$session['environnement']."/footer.php");

require_once(get_path_module("admin"));
$pc = new panneau_controle();
$pc->calculRubriqueParFonction($op);
$TPLN->Parse("panneau_controle",$pc->retourneEntete());

$TPLN->Write();
?>